package com.winning.zjjh.convert;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: xkh@winning.com.cn
 * @Desc: TODO
 * @Date: 2019-03-20 13:55
 **/
public class DateFormatAdapter extends XmlAdapter<String, Date> {

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public Date unmarshal(String v) throws Exception {
        Date date;
        try{
            date = simpleDateFormat.parse(v);
        }catch (Exception e) {
            date = null;
        }
        return date;
    }

    @Override
    public String marshal(Date v) throws Exception {
        String dateStr;
        try{
            dateStr = simpleDateFormat.format(v);
        }catch (Exception e) {
            dateStr = "";
        }
        return dateStr;
    }
}
